package com.estronger.shopmanage.ui.personal;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.BeanBase;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.utils.Toast;
import com.lidroid.xutils.http.RequestParams;

public class ChangePasswordActivity extends BaseActivity implements
		OnClickListener {

	private EditText et_new_psw;
	private EditText et_old_psw;
	private EditText et_new_psw_again;

	@Override
	protected void initView() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_change_psw);
		((RelativeLayout) findViewById(R.id.rl_back)).setOnClickListener(this);
		((Button) findViewById(R.id.btn_ok)).setOnClickListener(this);
		et_new_psw = (EditText) findViewById(R.id.et_new_psw);
		et_old_psw = (EditText) findViewById(R.id.et_old_psw);
		et_new_psw_again = (EditText) findViewById(R.id.et_new_psw_again);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_back:
			finish();
			break;
		case R.id.btn_ok:
			// 确认修改之前的判断 以及 提交请求
			String oldPWD = et_old_psw.getText().toString().trim();
			String newPWD = et_new_psw.getText().toString().trim();
			if (TextUtils.isEmpty(oldPWD)) {
				Toast.show(this, "当前登录密码不能为空", 2000);
				return;
			}
			if (TextUtils.isEmpty(newPWD)) {
				Toast.show(this, "新密码不能为空", 2000);
				return;
			}
			if (TextUtils.isEmpty(et_new_psw_again.getText().toString().trim())) {
				Toast.show(this, "确认新密码不能为空", 2000);
				return;
			}
			if (!(newPWD.equals(et_new_psw_again.getText().toString().trim()))) {
				Toast.show(this, "两次输入的新密码不一致", 2000);
				return;
			}

			if (newPWD.length() < 6 || newPWD.length() > 12) {
				Toast.show(this, "密码长度应该在6-12位之间", 2000);
				return;
			}
			RequestParams params = new RequestParams();
			params.addBodyParameter("regid", MyApplication.jpush_id);
			params.addBodyParameter("usercode", MyApplication.getUserCode());
			params.addBodyParameter("old_pwd", oldPWD);
			params.addBodyParameter("new_pwd", newPWD);
			params.addBodyParameter("sf_action", "modifypwd");
			showDialog("提交新密码中...", false);
			connect(params, NetManager.CG_PSWD);
			break;

		default:
			break;
		}
	}

	@Override
	protected void onNetSuccess(int code, String result) {
		super.onNetSuccess(code, result);
		if (code == NetManager.CG_PSWD) {
			BeanBase bean = gson.fromJson(result, BeanBase.class);
			if (bean.success) {
				Toast.show(this, "已成功修改密码", 2000);
				setResult(666);
				finish();
			} else {
				Toast.show(this, "修改密码失败", 2000);
			}
		}
	}

}
